package com.zch.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.dao.model.User;
import com.item.dao.model.UserExample;
import com.item.daoEx.model.UserEx;
import com.item.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;

import com.zch.dao.model.Dept;
import com.zch.dao.model.DeptExample;
import com.zch.dao.model.TradeExample;
import com.zch.dao.model.ZhikuType;
import com.zch.dao.model.ZhikuTypeExample;
import com.zch.daoEx.model.DeptEx;
import com.zch.daoEx.model.ZhikuTypeEx;
import com.zch.service.DeptService;

@RequestMapping("dept")
@Controller
@LoginFilter
public class DeptController extends CoreController{

	@Autowired
	private DeptService deptService;
	@Autowired
    private UserService userService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(String id) throws Exception{
		/*PaginationSupport.byPage(page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<DeptEx> list = deptService.selectList(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));*/
	    DeptExample example = new DeptExample();
        example.setOrderByClause("create_time asc");
        if(StringUtils.isNotBlank(id))
        {
            example.createCriteria().andParentEqualTo(id);
        }
        else
        {
            example.createCriteria().andParentEqualTo("0");
        }
        
        List<Dept> list = deptService.selectByExample(example);
        List<DeptEx> listEx = new ArrayList<DeptEx>();
        for (Dept dept : list) 
        {
            DeptEx deptEx = new DeptEx();
                BeanUtils.copyProperties(deptEx, dept);
            if(StringUtils.isBlank(id) || id.equals("0"))
            {
                deptEx.setState("closed");
            }
            listEx.add(deptEx);
        }
        
        if (StringUtils.isBlank(id))
        {
            DeptEx root = new DeptEx();
            root.setParent("-1");
            root.setId("0");
            root.setLevel(0);
            root.setDeptName("根目录");
            listEx.add(0, root);
        }
        return JSONUtils.serialize(listEx);
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(Dept dept){
		if (StringUtils.isBlank(dept.getId())){
		    dept.setId(dept.getDeptNo());
			deptService.insert(dept);
		}else{
			deptService.updateByPrimaryKeySelective(dept);
		}
		return "{\"success\":true}";
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		Dept dept = deptService.selectByPrimaryKey(id);
		return JSONUtils.serialize(dept);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			deptService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/findDeptNo")
    @ResponseBody
    public String findDeptNo(){
        List<DeptEx> list = deptService.findDeptNo();
        List<DeptEx> lists = new ArrayList<DeptEx>();
        for(DeptEx ex : list){
            if(ex.getParent().equals("0")){
                lists.add(ex);
            }
        }
        return JSONUtils.serialize(lists);
    }

   @RequestMapping("/findDeptName")
    @ResponseBody
    public String findDeptName(String id){
        List<DeptEx> list = deptService.findDeptName(id);
        return JSONUtils.serialize(list);
    }
   
   @RequestMapping("/isNoExist")
   @ResponseBody
   public String isNoExist(String value){
       DeptExample example = new DeptExample();
       example.createCriteria().andDeptNoEqualTo(value);
       int cnt = deptService.countByExample(example);
       if(cnt>0){
           return JSONUtils.serialize(new Ret(1)); 
       }        
       return JSONUtils.serialize(new Ret(0));
   }
   
   @RequestMapping("/isNameExist")
   @ResponseBody
   public String isNameExist(String value){
       DeptExample example = new DeptExample();
       example.createCriteria().andDeptNameEqualTo(value);
       int cnt = deptService.countByExample(example);
       if(cnt>0){
           return JSONUtils.serialize(new Ret(1)); 
       }        
       return JSONUtils.serialize(new Ret(0));
   }
   
   @RequestMapping("/findSecondDeptno")
   @ResponseBody
   public String findFirstDeptNo(String id){
       /*List<DeptEx> list = deptService.findDeptNo();
       List<DeptEx> first = new ArrayList<DeptEx>();
       List<DeptEx> second = new ArrayList<DeptEx>();
       for(DeptEx ex : list){
           if(ex.getParent().equals("0")){
               String no = ex.getDeptNo();
               first.add(ex);
               for(DeptEx ex2 : first){
                   if(ex2.getParent().equals(no)){
                       second.add(ex2);
                   }
               }
           }
       }*/
       Map<String, Object> map = new HashMap<String, Object>();
       map.put("id", id);
       DeptExample example = new DeptExample();
       example.createCriteria().andParentEqualTo(id);       
       List<Dept> first = deptService.selectByExample(example);
       List<Dept> second = new ArrayList<Dept>();
       for(Dept ex : first){
           if(ex.getParent().equals(id)){
               second.add(ex);
           }
       }
       
       return JSONUtils.serialize(second);
   }
   
   class MenuItem
   {
       public String id;
       public String text;
       public String parent;
   }
   
   @RequestMapping("/findDeptList")
   @ResponseBody
   public String findDeptList() throws Exception{
       DeptExample example = new DeptExample();
       List<Dept> list = deptService.selectByExample(example);
       List<Dept> lists = new ArrayList<Dept>();
       for(Dept ex : list){
           if(ex.getParent().equals("0") && ex.getLevel() == 1){
               lists.add(ex);
           }
       }
       
       List<String> deptIdArr = new ArrayList<String>();
       // sql - users
       /*UserExample userExample = new UserExample();*/
       
       /*List<User> users = userService.selectByExample(userExample);*/
       
       List<MenuItem> menuItems = new ArrayList<MenuItem>();
       for (Dept dept : lists)
       {
           MenuItem menuItem = new MenuItem();
           menuItem.id = dept.getDeptNo();
           menuItem.text = dept.getDeptName();
           menuItem.parent = "0";          
           UserExample userExample = new UserExample();
           userExample.createCriteria().andDeptNoEqualTo(menuItem.id);
           List<User> users = userService.selectByExample(userExample);
           for (User user : users)
           {
               MenuItem subItem = new MenuItem();
               subItem.id = user.getId();
               subItem.text = user.getNickName();
               subItem.parent = menuItem.id;
               menuItems.add(subItem);
           }
           if(users.size()>0){
               menuItems.add(menuItem);
           }
          
       }
       
       return JSONUtils.serialize(menuItems);
   }
}   